package com.wannengbang.cloudleader.homepage;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.DirectorListBean;
import com.wannengbang.cloudleader.bean.ShareIntoListBean;
import com.wannengbang.cloudleader.bean.ShareToBeBean;
import com.wannengbang.cloudleader.homepage.adapter.ShareBonusIntoListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.mine.DirectorListActivity;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.MaterialHeader;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareBonusActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.layout_title)
    LinearLayout layoutTitle;
    @BindView(R.id.tv_today_money)
    TextView tvTodayMoney;
    @BindView(R.id.tv_bonus_num)
    TextView tvBonusNum;
    @BindView(R.id.ll_bonus_num)
    LinearLayout llBonusNum;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.tv_name)
    TextView tv_name;
    private IHomePageModel homePageModel;

    private List<ShareIntoListBean.DataBean.ItemListBean> beanList;
    private ShareBonusIntoListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private boolean isSetBar;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_bonus);
        ButterKnife.bind(this);
        getImmersionBar().statusBarDarkFont(false).statusBarDarkFont(false, 0.2f).init();
        initView();
        initData();
        requestAgentDirector();
    }

    private void initView() {
        //1 董事 2 红包版运营中心分红 3创客
        type = getIntent().getStringExtra("type");

        if ("2".equals(type)){
            titleBar.setTitle("运营中心");
            tv_name.setText(">当前运营中心人数");
        }else if ("3".equals(type)){
            titleBar.setTitle("创客分红");
            tv_name.setText("当前创客总人数");
        }

        homePageModel = new HomePageModelImpl();
        refreshLayout.setRefreshHeader(new MaterialHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        titleBar.setRightClickListener(() -> {
            Intent intent = new Intent(mActivity, ShareBonusListActivity.class);
            intent.putExtra("type", type);
            startActivity(intent);

        });

        int height = (int) Dp2Px.dp2Px(181);
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 0) {   //设置标题的背景颜色
                    layoutTitle.setBackgroundColor(Color.argb((int) 0, 255, 255, 255));
                    viewLine.setBackgroundColor(Color.argb((int) 0, 243, 243, 243));
                    titleBar.setTitleColor(getResources().getColor(R.color.white));
                    titleBar.setTitleRightColor(getResources().getColor(R.color.white));
                    titleBar.setLeftIcon(R.mipmap.ic_back_white);
                    getImmersionBar().statusBarDarkFont(false).statusBarDarkFont(false, 0.2f).init();
                    isSetBar = false;
                } else if (scrollY > 0 && scrollY <= height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                    float scale = (float) scrollY / height;
                    float alpha = (255 * scale);
                    layoutTitle.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));
                    viewLine.setBackgroundColor(Color.argb((int) alpha, 243, 243, 243));
                } else {    //滑动到banner下面设置普通颜色
                    if (!isSetBar) {
                        layoutTitle.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                        viewLine.setBackgroundColor(Color.argb((int) 255, 243, 243, 243));
                        titleBar.setTitleColor(getResources().getColor(R.color.text_color_2));
                        titleBar.setTitleRightColor(getResources().getColor(R.color.text_color_2));
                        titleBar.setLeftIcon(R.mipmap.ic_back_black);
                        getImmersionBar().statusBarDarkFont(true).statusBarDarkFont(true, 0.2f).init();
                        isSetBar = true;
                    }
                }
            }
        });
    }

    private void initData() {
        beanList = new ArrayList<>();
        listAdapter = new ShareBonusIntoListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_active_list)
                .show();
        refreshData();
        requestDirectorBonusesToBe();
    }

    private void requestAgentDirector() {
        new MineModelImpl().requestAgentDirector(1,type, new DataCallBack<DirectorListBean>() {
            @Override
            public void onSuccessful(DirectorListBean bean) {
                if (bean.getData() != null && bean.getData().getPagination() != null) {
                    tvBonusNum.setText(bean.getData().getPagination().getTotal_count() + "");
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestDirectorBonusesToBe() {
        homePageModel.requestDirectorBonusesToBe(type,new DataCallBack<ShareToBeBean>() {
            @Override
            public void onSuccessful(ShareToBeBean bean) {
                String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getShare_bonus_count()) / 100);
                String todayMoney = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getShare_bonus_count_today()) / 100);
                tvTotalMoney.setText(money);
                tvTodayMoney.setText("+" + todayMoney);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestData() {
        homePageModel.requestDirectorBonusesIndex(page,type, new DataCallBack<ShareIntoListBean>() {
            @Override
            public void onSuccessful(ShareIntoListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }

    @OnClick(R.id.ll_bonus_num)
    public void onViewClicked() {
        Intent intent = new Intent(mActivity, DirectorListActivity.class);
        intent.putExtra("type", type);
        startActivity(intent);
    }
}
